var view;
var sum_btn;
var view_btn_sum = 16;
var view_line_sum = 4;
var install_random_chess;
var sum_number;
var course;
var view_course;
//随机出现的棋子
var random;
/*
 下面定义数学参数
 */
var math_array;
var music;

var zhang_say;
var zhang_say = true;
var tips_say;
var tips_count = 0;
var tips_count_waring = 10000;
/* 
初始化所有设置,调用onload进行加载
 */
function InstallOpstion() {
	InstallChessboard();
	CreateButtonSum();
	InstallOptionAndCourse();
}

/* 
矩阵
 0  1  2  3
 4  5  6  7
 8  9  10 11
 12 13 14 15
 */

/* 
 初始化棋盘
 加入listView:game-button-2048button
 */
function InstallChessboard() {
	view = document.getElementById('view');
	sum_btn = new Array();
	sum_number = new Array();
}

/* 
初始化操作和计分 
 */
function InstallOptionAndCourse() {
	course = 0;
	view_course = document.getElementById('course');
	view_course.innerHTML = course.toString();
}

function CreateButtonSum() {
	for (let i = 0; i < view_btn_sum; i++) {
		sum_btn[i] = document.createElement('div');
		sum_btn[i].classList.add("game-button-2048button");
		sum_btn[i].style.backgroundColor = '#E3EDCD'
		sum_btn[i].style.boxShadow = '0px 0px 30px 10px  #E3EDCD'
		view.appendChild(sum_btn[i])
	}
	// sum_btn[GetRandom()].innerHTML = '2';
}

function GetRandom() {
	return Math.floor(Math.random() * 16);
}

function getViewNumber() {
	for (let i = 0; i < view_btn_sum; i++) {
		sum_number[i] = sum_btn[i].innerHTML;
	}
	return sum_number;
}

/* 
矩阵
 0  1  2  3
 4  5  6  7
 8  9  10 11
 12 13 14 15
 */
function ActiceUP() {
	let temp = 0;
	for (let i = 0; i < view_btn_sum; i++) {
		if (i >= view_line_sum) {
			temp = BeginCountNumber(sum_btn[i - 4], sum_btn[i])
			if (temp == 404001) {
				// 扩展
			} else if (temp != 404001) {
				if (temp == 0) {
					sum_btn[i - 4].innerHTML = '';
				} else {
					sum_btn[i - 4].innerHTML = temp.toString();
				}
				sum_btn[i].innerHTML = '';
				course = temp;
				view_course.innerHTML = course.toString();
			}
		}
	}
	RandomShowNumber();
}

/* 
 抽出公共方法,获取传递两个按钮相加的结果
 返回404001代表无法合成
 否则就可以合成
 */
function BeginCountNumber(firtstr, endstr) {
	var array = new Array();
	if (firtstr.innerHTML == '') {
		array[0] = 0;
	} else {
		array[0] = Number.parseInt(firtstr.innerHTML);
	}
	if (endstr.innerHTML == '') {
		array[1] = 0;
	} else {
		array[1] = Number.parseInt(firtstr.innerHTML);
	}
	// 一样的数字可以合成,不一样的不可以合成
	if (array[0] == array[1]) {
		return array[0] + array[1];
	} else {
		return 404001;
	}
}


/* 
随机出现一个数字 
 */
function RandomShowNumber() {
	var list_array = new Array();
	for (let i = 0; i < view_btn_sum; i++) {
		if (sum_btn[i].innerHTML == '') {
			list_array.push(i)
		}
	}
	if (list_array.length != 0) {
		sum_btn[list_array[Math.floor((Math.random() * list_array.length))]].innerHTML = '2';
	}
}

/* 
按键事件
 enter = 13
 A = 65 left=37
 S = 83 up=38
 D = 68 right=39
 W = 87 down=40
 */
$(document).keydown(function() {
	if (event.keyCode == 13) {
		console.log(course)
	}
})
$(document).keydown(function() {
	if (event.keyCode == 65 || event.keyCode == 37) {
		LEFT();
		ShowArray();
	}
})
$(document).keydown(function() {
	if (event.keyCode == 83 || event.keyCode == 40) {
		BOTTOM();
		ShowArray();
	}
})
$(document).keydown(function() {
	if (event.keyCode == 68 || event.keyCode == 39) {
		RIGHT();
		ShowArray();
	}
})
$(document).keydown(function() {
	if (event.keyCode == 87 || event.keyCode == 38) {
		TOP();
		ShowArray();
	}
})








/* 纯数学实现 */
function Install() {
	GetMatrix();
	InstallOpstion()
}

/* 重置参数 */
function Reseting() {
	course = 0;
	view_course.innerHTML = 0;
	tips_count_waring = 10000;
	tips_count = 0;
	GetMatrix();
	ShowArray();
}

function TOP() {
	PlayMp3()
	PlayZhangSay()
	// var temp ;
	// for(let i=0 ;i<view_btn_sum;i++){
	// 	if(i>=view_line_sum){

	// 		从最后向上循环,查找能否合成 
	// 		(i%view_line_sum) 这个是余数;
	// 		(i%view_line_sum)*view_line_sum最后一个
	// 		((i%view_line_sum)-1)*view_line_sum这是前一个

	// 		for(let r = view_line_sum ;r>=1;r--){
	// 			if(math_array[ (i%view_line_sum)*view_line_sum]!=0 &&math_array[((i%view_line_sum)-1)*view_line_sum]!=0){
	// 				if(math_array[((i%view_line_sum)-1)*view_line_sum]==math_array[(i%view_line_sum)*view_line_sum]){
	// 					course += math_array[((i%view_line_sum)-1)*view_line_sum]+math_array[(i%view_line_sum)*view_line_sum];
	// 					math_array[((i%view_line_sum)-1)*view_line_sum] = math_array[((i%view_line_sum)-1)*view_line_sum]+math_array[(i%view_line_sum)*view_line_sum];
	// 					math_array[(i%view_line_sum)*view_line_sum] = 0;
	// 				}else if(((i%view_line_sum)-1)*view_line_sum==0){
	// 					math_array[((i%view_line_sum)-1)*view_line_sum] = (i%view_line_sum)*view_line_sum;
	// 				}
	// 				console.log("*&^&%^$&%^$%^%^"+(i%view_line_sum)*view_line_sum+"||"+((i%view_line_sum)-1)*view_line_sum);
	// 			}

	// 		}	
	// 	}		
	// }	

	for (let i = view_btn_sum - 1; i >= 0; i--) {
		if (i >= 4) {
			if (math_array[i] != 0 && math_array[i - view_line_sum] != 0) {
				if (math_array[i] == math_array[i - view_line_sum]) {
					course += math_array[i] + math_array[i - view_line_sum];
					math_array[i - view_line_sum] = math_array[i] + math_array[i - view_line_sum];
					math_array[i] = 0;
				}
			} else if (math_array[i - view_line_sum] == 0) {
				math_array[i - view_line_sum] = math_array[i];
				math_array[i] = 0;

			}
		}

	}
	for (let i = view_btn_sum - 1; i >= 0; i--) {
		if (i >= 4) {
			if (math_array[i] != 0 && math_array[i - view_line_sum] != 0) {
				if (math_array[i] == math_array[i - view_line_sum]) {
					course += math_array[i] + math_array[i - view_line_sum];
					math_array[i - view_line_sum] = math_array[i] + math_array[i - view_line_sum];
					math_array[i] = 0;
				}
			} else if (math_array[i - view_line_sum] == 0) {
				math_array[i - view_line_sum] = math_array[i];
				math_array[i] = 0;

			}
		}

	}
	for (let i = view_btn_sum - 1; i >= 0; i--) {
		if (i >= 4) {
			if (math_array[i] != 0 && math_array[i - view_line_sum] != 0) {
				if (math_array[i] == math_array[i - view_line_sum]) {
					course += math_array[i] + math_array[i - view_line_sum];
					math_array[i - view_line_sum] = math_array[i] + math_array[i - view_line_sum];
					math_array[i] = 0;
				}
			} else if (math_array[i - view_line_sum] == 0) {
				math_array[i - view_line_sum] = math_array[i];
				math_array[i] = 0;

			}
		}

	}
	RandomPoint();
}

function LEFT() {
	PlayMp3()
	PlayZhangSay()
	for (let i = view_btn_sum - 1; i >= 0; i--) {
		if (!(i % 4 == 0)) {
			if (math_array[i] != 0 && math_array[i - 1] != 0) {
				if (math_array[i] == math_array[i - 1]) {
					course += math_array[i] + math_array[i - 1];
					math_array[i - 1] = math_array[i] + math_array[i - 1];
					math_array[i] = 0;
				}
			} else if (math_array[i - 1] == 0) {
				math_array[i - 1] = math_array[i];
				math_array[i] = 0;

			}
		}

	}
	for (let i = view_btn_sum - 1; i >= 0; i--) {
		if (!(i % 4 == 0)) {
			if (math_array[i] != 0 && math_array[i - 1] != 0) {
				if (math_array[i] == math_array[i - 1]) {
					course += math_array[i] + math_array[i - 1];
					math_array[i - 1] = math_array[i] + math_array[i - 1];
					math_array[i] = 0;
				}
			} else if (math_array[i - 1] == 0) {
				math_array[i - 1] = math_array[i];
				math_array[i] = 0;

			}
		}

	}
	for (let i = view_btn_sum - 1; i >= 0; i--) {
		if (!(i % 4 == 0)) {
			if (math_array[i] != 0 && math_array[i - 1] != 0) {
				if (math_array[i] == math_array[i - 1]) {
					course += math_array[i] + math_array[i - 1];
					math_array[i - 1] = math_array[i] + math_array[i - 1];
					math_array[i] = 0;
				}
			} else if (math_array[i - 1] == 0) {
				math_array[i - 1] = math_array[i];
				math_array[i] = 0;

			}
		}

	}
	RandomPoint();
}

function RIGHT() {
	PlayMp3()
	PlayZhangSay()
	for (let i = 0; i < view_btn_sum; i++) {
		if (!(i % 4 == 3)) {
			if (math_array[i] != 0 && math_array[i + 1] != 0) {
				if (math_array[i] == math_array[i + 1]) {
					course += math_array[i] + math_array[i + 1];
					math_array[i + 1] = math_array[i] + math_array[i + 1];
					math_array[i] = 0;
				}
			} else if (math_array[i + 1] == 0) {
				math_array[i + 1] = math_array[i];
				math_array[i] = 0;

			}
		}

	}
	for (let i = 0; i < view_btn_sum; i++) {
		if (!(i % 4 == 3)) {
			if (math_array[i] != 0 && math_array[i + 1] != 0) {
				if (math_array[i] == math_array[i + 1]) {
					course += math_array[i] + math_array[i + 1];
					math_array[i + 1] = math_array[i] + math_array[i + 1];
					math_array[i] = 0;
				}
			} else if (math_array[i + 1] == 0) {
				math_array[i + 1] = math_array[i];
				math_array[i] = 0;

			}
		}

	}
	for (let i = 0; i < view_btn_sum; i++) {
		if (!(i % 4 == 3)) {
			if (math_array[i] != 0 && math_array[i + 1] != 0) {
				if (math_array[i] == math_array[i + 1]) {
					course += math_array[i] + math_array[i + 1];
					math_array[i + 1] = math_array[i] + math_array[i + 1];
					math_array[i] = 0;
				}
			} else if (math_array[i + 1] == 0) {
				math_array[i + 1] = math_array[i];
				math_array[i] = 0;

			}
		}

	}
	RandomPoint();
}

function BOTTOM() {
	PlayMp3()
	PlayZhangSay()
	for (let i = 0; i < view_btn_sum; i++) {
		if (i <= 11) {
			if (math_array[i] != 0 && math_array[i + view_line_sum] != 0) {
				if (math_array[i] == math_array[i + view_line_sum]) {
					course += math_array[i] + math_array[i + view_line_sum];
					math_array[i + view_line_sum] = math_array[i] + math_array[i + view_line_sum];
					math_array[i] = 0;
				}
			} else if (math_array[i + view_line_sum] == 0) {
				math_array[i + view_line_sum] = math_array[i];
				math_array[i] = 0;

			}
		}

	}
	for (let i = 0; i < view_btn_sum; i++) {
		if (i <= 11) {
			if (math_array[i] != 0 && math_array[i + view_line_sum] != 0) {
				if (math_array[i] == math_array[i + view_line_sum]) {
					course += math_array[i] + math_array[i + view_line_sum];
					math_array[i + view_line_sum] = math_array[i] + math_array[i + view_line_sum];
					math_array[i] = 0;
				}
			} else if (math_array[i + view_line_sum] == 0) {
				math_array[i + view_line_sum] = math_array[i];
				math_array[i] = 0;

			}
		}

	}
	for (let i = 0; i < view_btn_sum; i++) {
		if (i <= 11) {
			if (math_array[i] != 0 && math_array[i + view_line_sum] != 0) {
				if (math_array[i] == math_array[i + view_line_sum]) {
					course += math_array[i] + math_array[i + view_line_sum];
					math_array[i + view_line_sum] = math_array[i] + math_array[i + view_line_sum];
					math_array[i] = 0;
				}
			} else if (math_array[i + view_line_sum] == 0) {
				math_array[i + view_line_sum] = math_array[i];
				math_array[i] = 0;

			}
		}

	}
	RandomPoint();
}

/* 
 if(math_array[i]!=0 &&math_array[i-view_line_sum]!=0){
 	if(math_array[i]==math_array[i-view_line_sum]){
 		course += math_array[i]+math_array[i-view_line_sum];
 		math_array[i-view_line_sum] = math_array[i]+math_array[i-view_line_sum];
 		math_array[i] = 0;
 	}else if(math_array[i]==0){
 		math_array[i] = i-view_line_sum;
 	}
 }
 */

function RandomPoint() {

	var list_array = new Array();
	for (let i = 0; i < view_btn_sum; i++) {
		if (math_array[i] == 0) {
			list_array.push(i)
		}
	}
	if (list_array.length != 0) {
		//(Math.random()*list_array.length)
		random = list_array[Math.floor(Math.random() * list_array.length)];
		math_array[random] = 2;

	}
}

function GetMatrix() {
	music = new Audio('res/music/music.mp3')
	tips_say = document.getElementById('tips_count');
	tips_say.innerHTML = tips_count;
	math_array = new Array();
	for (let i = 0; i < view_btn_sum; i++) {
		math_array[i] = 0;
	}
	math_array[Math.floor(Math.random() * view_btn_sum)];
	RandomPoint()
	return math_array;
}

/* 
吧矩阵显示在网页上 
 */
function ShowArray() {
	for (var i = 0; i < view_btn_sum; i++) {

		if (math_array[i] == 0) {
			sum_btn[i].innerHTML = "";
		} else {
			sum_btn[i].innerHTML = math_array[i];
		}
		if (i == random) {
			$(sum_btn[random]).animate({
				'color': '#ffffff',
				"fontSize": "100px"
			}, 500, function() {})


			$(sum_btn[random]).animate({
				'color': '#4d474d',
				"fontSize": "40px"
			}, 500, function() {})
		}
		if (Number(sum_btn[i].innerHTML) > 2000) {
			sum_btn[i].style.backgroundColor = '#e20003'
		} else if (Number(sum_btn[i].innerHTML) > 1000) {
			sum_btn[i].style.backgroundColor = '#ff6208'
		} else if (Number(sum_btn[i].innerHTML) > 500) {
			sum_btn[i].style.backgroundColor = '#ff9500'
		} else if (Number(sum_btn[i].innerHTML) > 100) {
			sum_btn[i].style.backgroundColor = '#ffab03'
		} else if (Number(sum_btn[i].innerHTML) > 10) {
			sum_btn[i].style.backgroundColor = '#ffd900'
		} else {
			sum_btn[i].style.backgroundColor = '#E3EDCD'
		}
	}
	view_course.innerHTML = course;
	tips_say.innerHTML = tips_count;
}


/* 手势 */
var x_start = 0;
var y_start = 0;
var x_end = 0;
var y_end = 0;


function OnMouseDown() {
	x_start = event.pageX;
	y_start = event.pageY;
}

function OnMouseUp() {
	x_end = event.pageX;
	y_end = event.pageY;


	if (x_start < x_end && ((x_end - x_start) > (y_start - y_end)) && ((x_end - x_start) > (y_end - y_start))) {
		RIGHT();
		ShowArray();
	}

	if (x_start > x_end && ((x_start - x_end) > (y_start - y_end) && ((x_start - x_end) > (y_end - y_start)))) {
		LEFT();
		ShowArray();
	}


	if (y_start > y_end && ((y_start - y_end) > (x_start - x_end)) && ((y_start - y_end) > (x_end - x_start))) {
		TOP();
		ShowArray();
	}

	if (y_end > y_start && ((y_end - y_start) > (x_start - x_end)) && ((y_end - y_start) > (x_end - x_start))) {
		BOTTOM();
		ShowArray()
	}
}

// $(document).mousedown(function(event){
// 	x_start =event.pageX;
// 	y_start = event.pageY;
// })
// $(document).mouseup(function(event){
// 	x_end =event.pageX;
// 	y_end = event.pageY;


// 	if(x_start<x_end&&((x_end-x_start)>(y_start-y_end))&&((x_end-x_start)>(y_end-y_start))){
// 		RIGHT();
// 		ShowArray();
// 	}

// 	if(x_start>x_end&&((x_start-x_end)>(y_start-y_end)&&((x_start-x_end)>(y_end-y_start)))){
// 		LEFT();
// 		ShowArray();
// 	}


// 	if(y_start>y_end&& ((y_start-y_end)>(x_start-x_end)) &&((y_start-y_end)>(x_end-x_start))){
// 		TOP();
// 		ShowArray();
// 	}

// 	if(y_end>y_start&& ((y_end-y_start)>(x_start-x_end)) &&((y_end-y_start)>(x_end-x_start))){
// 		BOTTOM();
// 		ShowArray()
// 	}


// })


function PlayMp3() {
	music = null;
	music = new Audio('res/music/music.mp3')
	music.play()
}

function PlayZhangSay() {
	if (course > tips_count_waring) {
		tips_count += 1;
		tips_count_waring += 10000;
		zhang_say = null;
		zhang_say = new Audio('res/music/zhang_say.mp3')
		zhang_say.play();
	}
}



// 移动端适配
window.addEventListener('touchstart', function(event) {
	// 阻止事件

	x_start = event.touches[0].pageX;
	y_start = event.touches[0].pageY;
	event.preventDefault()
	console.log(x_start + "||" + y_start)
}, {
	passive: false
})

window.addEventListener("touchend", function(event) {

	x_end = event.changedTouches[0].pageX;
	y_end = event.changedTouches[0].pageY;


	if (x_start < x_end && ((x_end - x_start) > (y_start - y_end)) && ((x_end - x_start) > (y_end - y_start))) {
		RIGHT();
		ShowArray();
	}

	if (x_start > x_end && ((x_start - x_end) > (y_start - y_end) && ((x_start - x_end) > (y_end - y_start)))) {
		LEFT();
		ShowArray();
	}


	if (y_start > y_end && ((y_start - y_end) > (x_start - x_end)) && ((y_start - y_end) > (x_end - x_start))) {
		TOP();
		ShowArray();
	}

	if (y_end > y_start && ((y_end - y_start) > (x_start - x_end)) && ((y_end - y_start) > (x_end - x_start))) {
		BOTTOM();
		ShowArray()
	}
	event.preventDefault()
}, {
	passive: false
})
